package com.blog.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.user.entity.User;
import com.blog.user.entity.vo.UserVo;
import com.blog.user.mapper.UserMapper;
import com.blog.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zw
 * @since 2021-12-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void pageQuery(Page<User> pageInfo, String pet) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id","user_name","user_gender","user_register_time","user_freeze","user_description","user_pet","user_email");
        if (pet == null){
            queryWrapper.orderBy(true,true,"user_id");
            this.page(pageInfo,queryWrapper);
            return;
        }
        //有条件 条件判空处理
        if (!pet.isEmpty()){
            //name模糊查询
            queryWrapper.orderBy(true,true,"convert(user_pet using gbk)");
            queryWrapper.like("user_pet",pet);
        }
        this.page(pageInfo,queryWrapper);
    }

    @Override
    public User getOneByName(String userName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id","user_name","user_gender","user_register_time","user_freeze","user_description","user_pet","user_email")
                .eq("user_name",userName);
        User user = this.getOne(queryWrapper);
        return user;
    }

    @Override
    public UserVo getById(String id) {
        User user=super.getById(id);
        if(user==null)
            return null;
        UserVo userVo = new UserVo(user);
        userVo.setRole(getRoleByID(id));
        return userVo;
    }

    @Override
    public Integer getRoleByID(String id) {
        Integer role=userMapper.getRoleByID(id);
        return role;
    }

    @Override
    public void setRoleByID(String id, Integer role) {
        userMapper.updateRoleByID(id,role);
    }

}
